﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.LIB;

namespace HYElectricSystemSetting
{
	public partial class MoreLoopForm : System.Windows.Forms.Form
	{
		public MoreLoopForm()
		{
			this.InitializeComponent();
			this.btnDeleteItems.Image = Resource.GetImage("delete16");
		}

		private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
			{
				this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
				this.dataGridView1.BeginEdit(true);
			}
		}

		private void MoreLoopForm_Load(object sender, EventArgs e)
		{
			ToolTip toolTip = new ToolTip();
			toolTip.AutoPopDelay = 5000;
			toolTip.InitialDelay = 1000;
			toolTip.ReshowDelay = 50;
			toolTip.ShowAlways = true;
			toolTip.SetToolTip(this.btnAddItems, "添加");
			toolTip.SetToolTip(this.btnReName, "编辑");
			toolTip.SetToolTip(this.btnDeleteItems, "删除");
			if (ElectricSystemSettingData.LoopsCorrectionFactor == null || ElectricSystemSettingData.LoopsCorrectionFactor.Count == 0)
			{
				return;
			}
			this.lcfd = ElectricSystemSettingData.LoopsCorrectionFactor[0];
			this.dataGridView1.Rows.Add(this.lcfd.CorrectionFactorItems.Count);
			int num = 0;
			int num2 = 0;
			foreach (KeyValuePair<int, double> keyValuePair in this.lcfd.CorrectionFactorItems)
			{
				this.dataGridView1.Rows[num2].ReadOnly = true;
				this.dataGridView1.Rows[num].Cells[0].Value = keyValuePair.Key;
				this.dataGridView1.Rows[num].Cells[1].Value = keyValuePair.Value;
				num++;
				num2++;
			}
		}

		private void toolStripButton1_Click(object sender, EventArgs e)
		{
			this.dataGridView1.Rows.Add();
		}

		private void toolStripButton2_Click(object sender, EventArgs e)
		{
			this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);
		}

		private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			this.lcfd.CorrectionFactorItems.Clear();
			this.save();
		}

		private void btnAddItems_Click(object sender, EventArgs e)
		{
			if (ElectricSystemSettingData.DataGridViewAddRow(1, this.dataGridView1))
			{
				MoreLoopForm.moreLoopIsEdit = true;
				this.save();
			}
		}

		private void btnDeleteItems_Click(object sender, EventArgs e)
		{
			if (this.dataGridView1.Rows.Count >= 1 && this.dataGridView1.SelectedRows.Count > 0)
			{
				this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);
				MoreLoopForm.moreLoopIsEdit = true;
				this.save();
			}
		}

		public void save()
		{
			this.lcfd.CorrectionFactorItems.Clear();
			for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
			{
				if (this.dataGridView1.Rows[i].Cells[0].Value != null && this.dataGridView1.Rows[i].Cells[0].Value.ToString() != "" && !this.lcfd.CorrectionFactorItems.ContainsKey(Convert.ToInt32(this.dataGridView1.Rows[i].Cells[0].Value)))
				{
					this.lcfd.CorrectionFactorItems.Add(Convert.ToInt32(this.dataGridView1.Rows[i].Cells[0].Value), Convert.ToDouble(this.dataGridView1.Rows[i].Cells[1].Value));
				}
			}
			MoreLoopForm.moreLoopIsEdit = true;
		}

		private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
		{
			this.dataGridView1.FirstDisplayedScrollingRowIndex = e.RowIndex;
		}

		private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			this.dgvTxt = (DataGridViewTextBoxEditingControl)e.Control;
			this.dgvTxt.SelectAll();
			this.dgvTxt.KeyPress += this.Cells_KeyPress;
		}

		private void Cells_KeyPress(object sender, KeyPressEventArgs e)
		{
			ElectricSystemSettingData.keyPressXS(e, this.dgvTxt);
		}

		private void btnReName_Click(object sender, EventArgs e)
		{
			if (ElectricSystemSettingData.DatagridviewEditRow(1, this.dataGridView1))
			{
				MoreLoopForm.moreLoopIsEdit = true;
				this.save();
			}
		}

		private LoopsCorrectionFactorData lcfd;

		public static bool moreLoopIsEdit;

		public DataGridViewTextBoxEditingControl dgvTxt;
	}
}
